package project.meetemup.database;

import java.sql.SQLException;

import project.meetemup.model.Profile;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

/**
 * DataBaseHelper. Contains all the basic operations to deal with database.
 * @since 8/04/2012
 * @author João Paulo Mota
 */
public class DataBaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "meetemup.db";
	private static final int DATABASE_VERSION = 1;
	
	private Dao<Profile, Long> profileDao = null;
	
	public DataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		// TODO Auto-generated constructor stub
	}

	/**
	 * called when the app starts. Create the database, if it doesn't exists already
	 * TODO This class should not depend on database model and entities. Develop a way to create the tables dynamically.
	 */
	public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
		try {
			TableUtils.createTable(connectionSource, Profile.class);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void onUpgrade(SQLiteDatabase arg0, ConnectionSource arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}
	
	/**
	 * Get profile Dao singleton. 
	 * TODO This class should not depend on database model and entities. Develop a way to get DAOs dynamically
	 * @return
	 * @throws SQLException
	 */
	public Dao<Profile, Long> getProfileDao() throws SQLException {
		if (profileDao == null) {
			profileDao = getDao(Profile.class);
		}
		return profileDao;
	}

}
